package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-07-12
 * Time: 14:29
 */
public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书...");
        System.out.println("请输入要删除的书籍：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSized = bookList.getUsedSized();

        int pos = -1;
        int i = 0;
        for (; i < currentSized; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if (i == currentSized) {
            System.out.println("没有要删除的书");
            return;
        }

        for (int j = pos; j < currentSized - 1; j++) {
            Book book = bookList.getBook(j + 1);
            bookList.setBooks(j, book);
        }
        bookList.setUsedSized(currentSized - 1);
        System.out.println("删除成功");
    }
}
